<template>
    <div class='edit-popup'>
        <popup
            ref='popupRef'
            :title='popupTitle'
            :async='true'
            width='780px'
            @confirm='handleSubmit'
            @close='handleClose'
        >
            <el-form ref='formRef' :model='formData' label-width='90px' :rules='formRules'>
                <el-form-item label='名称' prop='name'>
                    <el-input v-model='formData.name' clearable placeholder='请输入名称' />
                </el-form-item>
                <el-form-item label='照片' prop='image'>
                    <material-picker :limit='1' v-model='formData.image' type='image' />
                    <!--                    <el-input v-model='formData.image' clearable placeholder='请输入照片' />-->
                </el-form-item>
                <el-form-item label='城市' prop='city'>
                    <el-cascader
                        style='width:280px;' v-model='formData.city_id' :options='cityData'
                    />
                </el-form-item>
                <el-form-item label='介绍' prop='introduce'>
                    <el-input
                        v-model='formData.introduce'
                        type='textarea'
                        rows='4'
                        clearable
                        placeholder='请输入介绍'
                    />
                </el-form-item>
                <el-form-item label='视频' prop='video'>
                    <material-picker :limit='1' v-model='formData.video' type='video' />
                    <!--                    <el-input v-model='formData.video' clearable placeholder='请输入视频' />-->
                </el-form-item>
                <el-form-item label='视频封面' prop='cover_image'>
                    <material-picker :limit='1' v-model='formData.cover_image'  />

                </el-form-item>
                <el-form-item label='是否推荐' prop='is_tuijian'>
                    <el-switch v-model='formData.is_tuijian' :active-value='1' :inactive-value='0' />
                </el-form-item>
                <el-form-item label='状态' prop='status'>
                    <el-select v-model='formData.status' placeholder='请选择审核状态'>
                        <el-option v-for='(item) in statusOption' :key='item.value' :label='item.label'
                                   :value='item.value' e3 />
                    </el-select>

                    <!--                    <el-input v-model="formData.status" clearable placeholder="请输入1待审核2审核通过3审核驳回" />-->
                </el-form-item>
                <!--                <el-form-item label='用户ID' prop='user_id'>-->
                <!--                    <el-input v-model='formData.user_id' clearable placeholder='请输入用户ID' />-->
                <!--                </el-form-item>-->
            </el-form>
        </popup>
    </div>
</template>

<script lang='ts' setup name='tourGuideEdit'>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiTourGuideAdd, apiTourGuideEdit, apiTourGuideDetail } from '@/api/tour_guide'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { getCityNew } from '@/api/article'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
// 1待审核2审核通过3审核驳回
const statusOption = [{ label: '待审核', value: 1 }, { label: '审核通过', value: 2 }, { label: '审核驳回', value: 3 }]

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑乡邻管理' : '新增乡邻管理'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    image: '',
    introduce: '',
    video: '',
    status: '',
    user_id: '',
    city_id: [],
    is_tuijian:0,
    cover_image:""
})

const cityData = ref([])
// 表单验证
const formRules = reactive<any>({})

async function initCityData() {
    const resp = await getCityNew({})
    if (!resp || !resp.length) return

    function patchMap(v: any) {
        if (!v.hasOwnProperty('children')) return
        if (!v.children || !v.children.length) return
        return v.children.map((item: any) => {
            const { id, name, merger_name } = item
            const data = {
                value: id.toString(), label: name, fullName: merger_name
            }
            // const children = patchMap(item)
            // if (children) data.children = children
            return data
        })
    }
    cityData.value = resp.map((item: any) => {
        const { id, name, merger_name: fullName } = item
        return {
            label: name, value: id.toString(), children: patchMap(item), fullName
        }
    })
}
initCityData()

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiTourGuideDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await apiTourGuideEdit(data)
        : await apiTourGuideAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
